#!/bin/bash
CLIENT[0]=client1.out
CLIENT[1]=client2.out
CLIENT[2]=client3.out
CLIENT[3]=client4.out
CLIENT[4]=client5.out
CLIENT[5]=client6.out
CLIENT[6]=client7.out
CLIENT[7]=client8.out

VECTOR=0.0
VECTOR2=0.0
SVECTOR2=0.0

SORT=0.0
SORT2=0.0
SSORT2=0.0

IO=0.0
IO2=0.0
SIO2=0.0

CONNECTION=0.0
CONNECTION2=0.0
SCONNECTION2=0.0

TOTAL=0.0
TOTAL2=0.0
STOTAL2=0.0

for ((LOOP = 0; LOOP < 8; LOOP++));
do
	COUNT=0;
	for word in $(tail -n 5 ${CLIENT[$LOOP]})
	do
	if [[ "$word" =~ ^[0-9]+$ ]]; then
		case $COUNT in
		0)
			VECTOR=$(echo "scale=9; $VECTOR + $word" | bc)
			VECTOR2=$(echo "scale=9; $word" | bc)
			let COUNT+=1
			;;
		1)
			VECTOR=$(echo "scale=9; $VECTOR + ($word/10^9)" | bc)
			VECTOR2=$(echo "scale=9; ($VECTOR2 + ($word/10^9))^2" | bc)
			SVECTOR2=$(echo "scale=9; $SVECTOR2 + $VECTOR2" | bc)
			let COUNT+=1 
			;;
		2)
			SORT=$(echo "scale=9; $SORT + $word" | bc)
			SORT2=$(echo "scale=9; $word" | bc)		
			let COUNT+=1
			;;
		3)
			SORT=$(echo "scale=9; $SORT + ($word/10^9)" | bc)
			SORT2=$(echo "scale=9; ($SORT2 + ($word/10^9))^2" | bc)
			SSORT2=$(echo "scale=9; $SSORT2 + $SORT2" | bc)
			let COUNT+=1 
			;;
		4)
			IO=$(echo "scale=9; $IO + $word" | bc)	
			IO2=$(echo "scale=9; $word" | bc)	
			let COUNT+=1
			;;
		5)
			IO=$(echo "scale=9; $IO + ($word/10^9)" | bc)
			IO2=$(echo "scale=9; ($IO2 + ($word/10^9))^2" | bc)
			SIO2=$(echo "scale=9; $SIO2 + $IO2" | bc)
			let COUNT+=1 
			;;
		6)
			CONNECTION=$(echo "scale=9; $CONNECTION + $word" | bc)	
			CONNECTION2=$(echo "scale=9; $word" | bc)
			let COUNT+=1
			;;
		7)
			CONNECTION=$(echo "scale=9; $CONNECTION + ($word/10^9)" | bc)
			CONNECTION2=$(echo "scale=9; ($CONNECTION2 + ($word/10^9))^2" | bc)
			SCONNECTION2=$(echo "scale=9; $SCONNECTION2 + $CONNECTION2" | bc)
			let COUNT+=1 
			;;
		8)
			TOTAL=$(echo "scale=9; $TOTAL + $word" | bc)	
			TOTAL2=$(echo "scale=9; $word" | bc)	
			let COUNT+=1
			;;
		9)
			TOTAL=$(echo "scale=9; $TOTAL + ($word/10^9)" | bc)
			TOTAL2=$(echo "scale=9; ($TOTAL2 + ($word/10^9))^2" | bc)
			STOTAL2=$(echo "scale=9; $STOTAL2 + $TOTAL2" | bc)
			let COUNT+=1 
			;;
		esac	
	fi
	done
done

DVECTOR=$(echo "scale=9; sqrt(($SVECTOR2 - ($VECTOR^2)/8) / 7)" | bc | sed 's/^\./0./')
DSORT=$(echo "scale=9; sqrt(($SSORT2 - ($SORT^2)/8) / 7)" | bc | sed 's/^\./0./')
DIO=$(echo "scale=9; sqrt(($SIO2 - ($IO^2)/8) / 7)" | bc | sed 's/^\./0./')
DCONNECTION=$(echo "scale=9; sqrt(($SCONNECTION2 - ($CONNECTION^2)/8) / 7)" | bc | sed 's/^\./0./')
DTOTAL=$(echo "scale=9; sqrt(($STOTAL2 - ($TOTAL^2)/8) / 7)" | bc | sed 's/^\./0./')

VECTOR=$(echo "scale=9; $VECTOR/8" | bc | sed 's/^\./0./')	
SORT=$(echo "scale=9; $SORT/8" | bc | sed 's/^\./0./')
IO=$(echo "scale=9; $IO/8" | bc | sed 's/^\./0./')
CONNECTION=$(echo "scale=9; $CONNECTION/8" | bc | sed 's/^\./0./')
TOTAL=$(echo "scale=9; $TOTAL/8" | bc | sed 's/^\./0./')


echo "Média do tempo para iniciar o vetor: ${VECTOR}s. Desvio Padrão: ${DVECTOR}s."
echo "Média do tempo para ordenar o vetor: ${SORT}s. Desvio Padrão: ${DSORT}s."
echo "Média do tempo para exibir o vetor:  ${IO}s. Desvio Padrão: ${DIO}s."
echo "Média do tempo de conexão:           ${CONNECTION}s. Desvio Padrão: ${DCONNECTION}s."
echo "Média do tempo total:                ${TOTAL}s. Desvio Padrão: ${DTOTAL}s."
